package com.weds.edjk.web;

import com.alibaba.fastjson.JSONObject;
import com.weds.core.annotation.Logs;
import com.weds.core.base.BaseController;
import com.weds.core.resp.JsonResult;
import com.weds.edjk.entity.KtRecordEntity;
import com.weds.edjk.entity.UserFxEntity;
import com.weds.edjk.service.KtRecordService;
import com.weds.edjk.service.PersonMonitorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author zcz
 * @Description 人员基本信息管理
 * @Date 2020-6-12
 */
@RestController
@RequestMapping(value = "/person/monitor")
@Api(value = "人员基本信息管理", description = "人员基本信息管理")
public class PersonMonitorController extends BaseController {

    private Logger log = LogManager.getLogger();

    @Autowired
    KtRecordService ktRecordService;
    @Autowired
    PersonMonitorService personMonitorService;

    /**
     * 今日统计进出信息
     * @return
     */
    @Logs
    @ApiOperation(value = "今日统计进出信息", notes = "今日统计进出信息")
    @RequestMapping(value = "/countTodayFx", method = RequestMethod.GET)
    public @ResponseBody JSONObject countTodayFx() {
        JSONObject jsonObject = new JSONObject();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        Date todayBegin = null;
        try {
            todayBegin = sdf.parse(date + " 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //进
        short in = 1;
        //出
        short out = 2;
        int inCount = ktRecordService.countTodayFx(todayBegin,in);
        int outCount = ktRecordService.countTodayFx(todayBegin,out);
        jsonObject.put("inCount",inCount);
        jsonObject.put("outCount",outCount);
        return jsonObject;
    }

    /**
     * 今日出入历史记录
     * @return
     */
    @Logs
    @ApiOperation(value = "今日出入历史记录", notes = "今日出入历史记录")
    @RequestMapping(value = "/todayRecord", method = RequestMethod.GET)
    public @ResponseBody JSONObject todayRecord() {
        JSONObject jsonObject = new JSONObject();
        SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
        String date = sdf.format(new Date());
        Date todayBegin = null;
        try {
            todayBegin = sdf.parse(date + " 00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        List<UserFxEntity> list = ktRecordService.selectTodayFx(todayBegin);
        if(list != null && list.size() != 0){
            for (UserFxEntity entity : list) {
                entity.setCompletePhotoPath(personMonitorService.getPhotoPath(entity.getUserSerial()+""));
            }
        }
        jsonObject.put("list",list);
        jsonObject.put("count",list != null ? list.size() : 0);
        return jsonObject;
    }

    /**
     * 获取当前人员监控信息
     * @return
     */
    @Logs
    @ApiOperation(value = "获取当前人员监控信息", notes = "获取当前人员监控信息")
    @RequestMapping(value = "/getCurrentMonitor", method = RequestMethod.GET)
    public JsonResult<Object> getCurrentMonitor() {
        Map<String, Object> currentMonitor = personMonitorService.getCurrentMonitor();
        return succMsgData(currentMonitor);
    }

    /**
     * 今日访问分类
     * @return
     */
    @Logs
    @ApiOperation(value = "今日访问分类", notes = "今日访问分类")
    @RequestMapping(value = "/todayVisits", method = RequestMethod.GET)
    public JsonResult<Object> getTodayVisits() {
        Map<String, Object> map = personMonitorService.getTodayVisits();
        return succMsgData(map);
    }

    /**
     * 今日预警列表
     * @return
     */
    @Logs
    @ApiOperation(value = "今日预警列表", notes = "今日预警列表")
    @RequestMapping(value = "/warn", method = RequestMethod.GET)
    public JsonResult<Object> selectWarn() {
        List<KtRecordEntity> list = personMonitorService.selectWarn();
        return succMsgData(list);
    }
}